package com.tau.egonetwork.gui.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.CloseWindowListener;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import com.tau.egonetwork.common.Log;
import com.tau.egonetwork.gui.interfaces.IPopupResponse;

public class PopupGUI {

	//to deprecate this version!:
	public static void displayBrowser(String url, String title, final IPopupResponse popupResponse) {
		displayBrowser(null, url, title, popupResponse, 650, 750);
	}
	
	public static void displayBrowser(Display display, String url, String title, final IPopupResponse popupResponse, int width, int height) {
		
		final Shell shell = new Shell(display, SWT.CLOSE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		shell.setLayout(gridLayout);
		shell.setSize(width, height);
		shell.setText(title);
		
	    Monitor primary = display.getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		
		GridData data;
			    
		data = new GridData();
		data.horizontalSpan = 3;
		
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.horizontalSpan = 2;
		data.grabExcessHorizontalSpace = true;

		popupResponse.registerPopup(shell);
		
		//remove current info (for instance if a Facebook user was
		//just entered to a browser, erase that session so a new
		//user can be entered and so on).
		Browser.clearSessions();

		final Browser browser;
		try {
			browser = new Browser(shell, SWT.NONE);
		} catch (SWTError e) {
			Log.writeLineToLog("Could not instantiate Browser: " + e.getMessage());
			shell.dispose();
			return;
		}
						
		browser.addLocationListener(new LocationListener() {
			
			@Override
			public void changing(LocationEvent event) {

			}
			
			@Override
			public void changed(LocationEvent event) {
				if (popupResponse.onPopupResponse(event.location)) {
					event.doit = false;
					shell.dispose();
				}
			}
		});
		browser.addCloseWindowListener(new CloseWindowListener() {
			
			@Override
			public void close(WindowEvent event) {
				shell.dispose();
			}
		});
		
		final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.horizontalSpan = 3;
		progressBar.setLayoutData(data);
		
		browser.addProgressListener(new ProgressListener() {
			@Override
			public void changed(ProgressEvent event) {
					if (event.total == 0) return;                            
					int ratio = event.current * 100 / event.total;
					progressBar.setSelection(ratio);
			}
			@Override
			public void completed(ProgressEvent event) {
				progressBar.setSelection(0);
			}
		});
		
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.FILL;
		data.horizontalSpan = 3;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		browser.setLayoutData(data);

		shell.open();
		browser.setUrl(url);
		
	}
}
